package com.pan.hello.mysql.jdbc;

import com.pan.entity.Person;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class HelloMybatis {

    /**
     * 获取连接
     * @return
     */
    public static SqlSessionFactory getConnection(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }

    public static void main(String[] args) {

        SqlSessionFactory sqlSessionFactory = getConnection();

        SqlSession sqlSession=sqlSessionFactory.openSession();
        try{
            Long id=1L;
            Person person=sqlSession.selectOne("mapper.personMapper.findById",id);
            log.info(person.toString());

        }finally {
            sqlSession.close();
        }
    }

}
